package com.ad.admin.interview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 39466 on 2016/4/15.
 */
public class BaseActivity  extends AppCompatActivity {
    private Button btn[] = new Button[3];
    private TextView top_title;
    private Class[] targetActivities = new Class[]{MainActivity.class, QuestionActivity.class, SearchActivity.class};
    private int[] bottomImgs = new int[]{R.drawable.bottombtn01b, R.drawable.bottombtn02b, R.drawable.bottombtn03b};
    private int[] bottomImgsSelected = new int[]{R.drawable.bottombtn01a, R.drawable.bottombtn02a, R.drawable.bottombtn03a};
    private int i;

    private static List<Activity> allActivites = new ArrayList<>();
    private Button settingbtn;
    private PopupWindow window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        allActivites.add(this);
        super.onCreate(savedInstanceState);


    }
    protected void init(String title , int index){
        btn[0] = (Button)findViewById(R.id.home_btn);
        btn[1] = (Button)findViewById(R.id.question_btn);
        btn[2] = (Button)findViewById(R.id.search_btn);
        top_title = (TextView)findViewById(R.id.head_title);
        top_title.setText(title);
        for(i = 0 ; i < btn.length ; i++){
            final int temp = i;
            if( index == i){
                btn[i].setBackgroundResource(bottomImgsSelected[i]);
            }else{
                btn[i].setBackgroundResource(bottomImgs[i]);
                btn[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent in = new Intent(BaseActivity.this,targetActivities[temp]);
                        startActivity(in);
                    }
                });
            }
        }
        settingbtn = (Button)findViewById(R.id.setting_btn);

        settingbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                window = new PopupWindow(Globals.SCREEN_WIDTH/4,Globals.SCREEN_HEIGHT/6);
                View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.window_list,null);

                window.setContentView(view);
                Log.d("提示信息", settingbtn.toString()+Globals.SCREEN_WIDTH/4+" "+Globals.SCREEN_HEIGHT/6);
                //点击空白处消失
                window.setBackgroundDrawable(new BitmapDrawable());
                window.setFocusable(true);
                window.showAsDropDown(settingbtn);
                /*if(window.isShowing()) {
                    window.dismiss();
                }else {
                    //在设置按钮下方展示

                }*/
                TextView version = (TextView) view.findViewById(R.id.win_version);
                TextView about = (TextView) view.findViewById(R.id.win_about);
                TextView exit = (TextView) view.findViewById(R.id.win_exit);

                version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        Toast.makeText(BaseActivity.this, "当前为1.0.1版本", Toast.LENGTH_LONG).show();
                    }
                });

                about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                        builder.setTitle("提示信息");
                        builder.setMessage("此应用为北航南通团队制作");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.create().show();
                    }
                });

                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("提示", allActivites.size() + "-------------");
                        window.dismiss();
                        for(Activity a : allActivites) {
                            a.finish();
                        }
                    }
                });

            }
        });


    }

    @Override
    protected void onDestroy() {
        allActivites.remove(this);
        super.onDestroy();
    }
}
